import {
	requestAndroidPermission,
	gotoAppPermissionSetting
} from "@/js_sdk/wa-permission/permission.js"

export const LOCATION = "location"
export const CAMERA = "camera"
export const READ_STORAGE = "readStorage"
export const WRITE_STORAGE = "writeStorage"
export const CALL_PHONE = "call_phone"

const andriods = {
	"location": "android.permission.ACCESS_FINE_LOCATION", //位置权限
	"camera": "android.permission.CAMERA", //摄像头权限
	"readStorage": "android.permission.READ_EXTERNAL_STORAGE", //外部存储(含相册)读取权限
	"writeStorage": "android.permission.WRITE_EXTERNAL_STORAGE", //外部存储(含相册)写入权限
	"call_phone": "android.permission.CALL_PHONE", //拨打电话权限
}

// 检查授权
export const checkAppPermissions = async (permissionId) => {
	let pid = [];
	// #ifdef APP-PLUS
	permissionId.forEach(item => {
		if (andriods[item]) {
			pid.push(andriods[item])
		}
	})
	// #endif
	return new Promise((resolve, reject) => {
		if (pid.length > 0) {
			requestAndroidPermission(pid).then(res => {
				if(res === 1) {
					resolve(1)
					return
				}
				if (res === -1) {
					uni.showModal({
						title: '',
						content: '请在设置中打开相应权限',
						showCancel: true,
						success: res => {
							if (res.confirm) {
								gotoAppPermissionSetting()
							}
						}
					});
				}
			})
		}
	})

}